/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author NGOCLINH-PC
 */
public class Member {

    private int MemberID;
    private String MemberName;
    private String Username;
    private String Password;
    private String Email;
    private String Phone;
    private String Gender;
    private String Address;
    private Date Birthday;
    private Date DateCreate;
    private int RoleID;
    private int Status;
    private int job;

    public Member() {
    }

    public Member(int MemberID, String MemberName, String Username, String Password, String Email, String Phone, String Gender, String Address, Date Birthday, Date DateCreate, int RoleID, int Status, int job) {
        this.MemberID = MemberID;
        this.MemberName = MemberName;
        this.Username = Username;
        this.Password = Password;
        this.Email = Email;
        this.Phone = Phone;
        this.Gender = Gender;
        this.Address = Address;
        this.Birthday = Birthday;
        this.DateCreate = DateCreate;
        this.RoleID = RoleID;
        this.Status = Status;
        this.job = job;
    }

    public int getMemberID() {
        return MemberID;
    }

    public void setMemberID(int MemberID) {
        this.MemberID = MemberID;
    }

    public String getMemberName() {
        return MemberName;
    }

    public void setMemberName(String MemberName) {
        this.MemberName = MemberName;
    }

    public String getUsername() {
        return Username;
    }

    public void setUsername(String Username) {
        this.Username = Username;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

    public String getPhone() {
        return Phone;
    }

    public void setPhone(String Phone) {
        this.Phone = Phone;
    }

    public String getGender() {
        return Gender;
    }

    public void setGender(String Gender) {
        this.Gender = Gender;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String Address) {
        this.Address = Address;
    }

    public Date getBirthday() {
        return Birthday;
    }

    public void setBirthday(Date Birthday) {
        this.Birthday = Birthday;
    }

    public Date getDateCreate() {
        return DateCreate;
    }

    public void setDateCreate(Date DateCreate) {
        this.DateCreate = DateCreate;
    }

    public int getRoleID() {
        return RoleID;
    }

    public void setRoleID(int RoleID) {
        this.RoleID = RoleID;
    }

    public int getStatus() {
        return Status;
    }

    public void setStatus(int Status) {
        this.Status = Status;
    }

    public int getjob() {
        return job;
    }

    public void setjob(int job) {
        this.job = job;
    }

    public static void Minsert(String MemberName, String Username, String Password, String Email, String phone, String Gender, String Address, String birthday, String DateCreate, int RoleID, int Status, int job) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("INSERT INTO Member VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
            st.setString(1, MemberName);
            st.setString(2, Username);
            st.setString(3, Password);
            st.setString(4, Email);
            st.setString(5, phone);
            st.setString(6, Gender);
            st.setString(7, Address);
            st.setString(8, birthday);
            st.setString(9, DateCreate);
            st.setInt(10, RoleID);
            st.setInt(11, Status);
            st.setInt(12, job);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "INSERT Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Mupdate(int MemberID, int RoleID, int job, int Status) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("update Member SET RoleID=?,Status=?,job=? WHERE MemberID=?");
            st.setInt(1, RoleID);
            st.setInt(2, Status);
            st.setInt(3, job);
            st.setInt(4, MemberID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Update Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void ChangePass(String Query) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement(Query);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Update Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void MupdateProfile(int MemberID, String MemberName, String pass, String Email, String phone, String Gender, String Address, String birthday) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("update Member SET MemberName=?,Password=?, Email=?,Phone=?,Gender=?,Address=?,Birthday=? WHERE MemberID=?");
            st.setString(1, MemberName);
            st.setString(2, pass);
            st.setString(3, Email);
            st.setString(4, phone);
            st.setString(5, Gender);
            st.setString(6, Address);
            st.setString(7, birthday);
            st.setInt(8, MemberID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Update Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void MupdateProfilecl(int MemberID, String MemberName, String Email, String phone, String Gender, String Address, String birthday) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("update Member SET MemberName=?, Email=?,Phone=?,Gender=?,Address=?,Birthday=? WHERE MemberID=?");
            st.setString(1, MemberName);
            st.setString(2, Email);
            st.setString(3, phone);
            st.setString(4, Gender);
            st.setString(5, Address);
            st.setString(6, birthday);
            st.setInt(7, MemberID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Update Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Mdelete(int MemberID) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE FROM Member WHERE MemberID=?");
            st.setInt(1, MemberID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "DELETE Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Member> Mselect() {
        try {
            ArrayList<Member> listRole = new ArrayList<Member>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Member where RoleID=1 or RoleID=2");
            while (rs.next()) {
                Member mem = new Member(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getDate(9), rs.getDate(10), rs.getInt(11), rs.getInt(12), rs.getInt(13));
                listRole.add(mem);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Member> CustomerSelect() {
        try {
            ArrayList<Member> listRole = new ArrayList<Member>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Member where RoleID=3");
            while (rs.next()) {
                Member mem = new Member(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getDate(9), rs.getDate(10), rs.getInt(11), rs.getInt(12), rs.getInt(13));
                listRole.add(mem);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Member> MselectById(int id) {
        try {
            ArrayList<Member> listRole = new ArrayList<Member>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Member where MemberID='" + id + "'");
            while (rs.next()) {
                Member mem = new Member(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getDate(9), rs.getDate(10), rs.getInt(11), rs.getInt(12), rs.getInt(13));
                listRole.add(mem);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Member> MselectByquery(String query) {
        try {
            ArrayList<Member> listRole = new ArrayList<Member>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                Member mem = new Member(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getDate(9), rs.getDate(10), rs.getInt(11), rs.getInt(12), rs.getInt(13));
                listRole.add(mem);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Member> MselectByUser(String id) {
        try {
            ArrayList<Member> listRole = new ArrayList<Member>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Member where Username='" + id + "'");
            while (rs.next()) {
                Member mem = new Member(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getDate(9), rs.getDate(10), rs.getInt(11), rs.getInt(12), rs.getInt(13));
                listRole.add(mem);
            }
            return listRole;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static boolean checkLogin(String username, String password) {
        try {
            try {
                PreparedStatement st = Connect.Connect.getCon().prepareStatement("Select * from Member where Username=? AND Password=? AND Status=1 AND RoleID=1 or RoleID=2");
                st.setString(1, username);
                st.setString(2, password);
                ResultSet rs = st.executeQuery();
                if (rs.next()) {
                    return true;
                } else {
                    return false;
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;

    }

    public static boolean checkLogincl(String username, String password) {
        try {
            try {
                PreparedStatement st = Connect.Connect.getCon().prepareStatement("Select * from Member where  Username=? AND Password=? AND Status=1");
                st.setString(1, username.trim());
                st.setString(2, password);
                ResultSet rs = st.executeQuery();
                if (rs.next()) {
                    return true;
                } else {
                    return false;
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;

    }
}
